package com.topnav.mc.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.js.lib.result.Result;
import com.topnav.mc.admin.bean.OrgTreeSelectBean;
import com.topnav.mc.admin.entity.Authorization;
import com.topnav.mc.admin.entity.RoleAuthorization;
import com.topnav.mc.admin.entity.UserOrganizaiton;
import com.topnav.mc.admin.entity.bean.RoleAuthorizationBean;
import com.topnav.mc.admin.entity.param.RoleAuthorizationParam;
import com.topnav.mc.admin.entity.wrapper.RoleAuthorizationWrapper;
import com.topnav.mc.admin.security.PermissionGrade;
import com.topnav.mc.admin.service.IAuthorizationService;
import com.topnav.mc.admin.service.IRoleAuthorizationService;
import com.topnav.mc.admin.service.IUserOrganizaitonService;
import com.topnav.mc.base.mc.McController;
import com.topnav.mc.exception.ExceptionMsg;
import com.topnav.mc.uitls.ExceptionUtil;
import com.topnav.mc.uitls.PermissionGradeUtil;
import com.topnav.mc.validator.ValidList;
import com.topnav.mc.validator.group.IAdd;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: kenny
 * @Date: 2024/2/27 10:39
 */
@RestController
@CrossOrigin
@RequestMapping("admin/role/auth")
public class RoleAuthorizationController extends McController<RoleAuthorization, RoleAuthorizationBean, RoleAuthorizationParam,
        IRoleAuthorizationService, RoleAuthorizationWrapper> {

    @Resource
    private PermissionGradeUtil permissionGradeUtil;
    @Autowired
    private IRoleAuthorizationService roleAuthorizationService;
    @Resource
    private PermissionGradeUtil pmGradeUtil;
    @Autowired
    private IUserOrganizaitonService userOrganizaitonService;
    @Autowired
    private IAuthorizationService authorizationService;

    /**
     * 保存权限设备，批量保存
     * @param params
     * @return
     */
    @PostMapping("/batch/update")
    @ResponseBody
    public Result batchAdds(@RequestBody List<RoleAuthorization> params) {
        PermissionGrade permissionGrade = permissionGradeUtil.getPmGradeCheck();
        //客户端上传的roleId是同一个
        Long roleId = null;
        for(RoleAuthorization ra : params){
            if (roleId == null){
                roleId = ra.getRoleId();
            }
            ra.setUpdateTime(new Date());
            ra.setCreateTime(new Date());
            ra.setUpdateBy(permissionGrade.getUserName());
        }
        LambdaUpdateWrapper<RoleAuthorization> wrapper = new LambdaUpdateWrapper<RoleAuthorization>().eq(RoleAuthorization::getRoleId,roleId);
        roleAuthorizationService.remove(wrapper);

        roleAuthorizationService.saveBatch(params,params.size());
        return Result.ok();
    }



}
